import OpenAI from "openai";
import dotenv from 'dotenv';  // 用于管理环境变量

// 加载环境变量
dotenv.config();

const openai = new OpenAI({
    baseURL: 'https://api.deepseek.com/v1',  // 添加 v1 路径
    apiKey: process.env.DEEPSEEK_API_KEY,
    defaultHeaders: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${process.env.DEEPSEEK_API_KEY}`
    }
});

async function main() {
    try {
        if (!process.env.DEEPSEEK_API_KEY) {
            throw new Error('DEEPSEEK_API_KEY 未在环境变量中设置');
        }
        
        const completion = await openai.chat.completions.create({
            messages: [{ role: "system", content: "You are a helpful assistant." }],
            model: "deepseek-coder",  // 使用正确的模型名称
            max_tokens: 500,
            temperature: 0.7
        });
        
        console.log('API 响应:', completion.choices[0].message.content);
    } catch (error) {
        console.error('API 调用失败:', error.message);
        if (error.response) {
            console.error('错误响应:', error.response.data);
        }
    }
}

main();